// import { el } from 'element-plus/es/locale'
import { defineStore } from 'pinia'
import { computed, ref } from "vue"
import { userUserStore } from '@/stores/user'
import { getCartData, addCartData, delCartData, mergeCartData } from '@/apis/card'
export const useCardStore = defineStore('cart', () => {
  // 1. 定义state - cartList
  const cartList = ref([])
  // 2. 定义action - addCart
  const addCart = async (goods) => {
    // console.log('添加', goods)
    const userStore = userUserStore()
    const isLogin = computed(() => userStore.userInfo.token)
    if (isLogin.value) {
      await addCartData(goods)
      const res = await getCartData()
      if (res.data !== null){
        cartList.value = res.data
      }else{
        cartList.value = []
      }
    } else {
      if (goods.skuId === 0) {
        const item = cartList.value.find((item) => goods.goodsId === item.goodsId)
        if (item) {
          // 找到了
          item.count = item.count + goods.count
        } else {
          // 没找到
          cartList.value.push(goods)
        }
      } else {
        const item = cartList.value.find((item) => goods.skuId === item.skuId)
        if (item) {
          // 找到了
          item.count = item.count + goods.count
        } else {
          // 没找到
          cartList.value.push(goods)
        }
      }
    }



  }

  const delCart = async (goods) => {
    const userStore = userUserStore()
    const isLogin = computed(() => userStore.userInfo.token)
    if (isLogin.value) {
      await delCartData(goods)
      const res = await getCartData()
      if (res.data !== null){
        cartList.value = res.data
      }else{
        cartList.value = []
      }
    } else {
      if (goods.skuId === 0) {
        const indexId = cartList.value.findIndex(item => item.goodsId === goods.goodsId)
        cartList.value.splice(indexId, 1)
      } else {
        const indexId = cartList.value.findIndex(item => item.skuId === goods.skuId)
        cartList.value.splice(indexId, 1)
      }
    }
  }

  const updatePayCard = async() => {
    // const userStore = userUserStore()
    // const isLogin = computed(() => userStore.userInfo.token)
    // if (isLogin.value) {}
    const dellist = cartList.value.filter(item => item.isCheck === true)
    dellist.forEach(async(item) => {
      await delCartData(item)
    })
    // renew()
    cartList.value = cartList.value.filter(item => item.isCheck !== true)

  }

  // const renew = async() => {
  //   const res = await getCartData()
  //   if (res.data !== null){
  //     cartList.value = res.data
  //   }else{
  //     cartList.value = []
  //   }
  // }


  const updateCheck = (goods, selected) => {
    if (goods.skuId === 0) {
      const item = cartList.value.find(item => item.goodsId === goods.goodsId)
      item.isCheck = selected
    } else {
      const item = cartList.value.find(item => item.skuId === goods.skuId)
      item.isCheck = selected
    }
  }

  const updateCheckAll = () => {
    cartList.value.forEach(item => { item.isCheck = true })
  }

  const clearCart = () => {
    cartList.value = []
  }

  const mergeCart = async() => {
    await mergeCartData(cartList.value)
    const res = await getCartData()
    if (res.data !== null){
      cartList.value = res.data
    }else{
      cartList.value = []
    }
    
  }


  const allCount = computed(() => cartList.value.reduce((a, c) => a + c.count, 0))
  const allPrice = computed(() => cartList.value.reduce((a, c) => a + c.count * c.price, 0))
  const isAll = computed(() => cartList.value.every((item) => item.isCheck))
  const checkAllCount = computed(() => cartList.value.filter(item => item.isCheck).reduce((a, c) => a + c.count, 0))
  const checkAllPrice = computed(() => cartList.value.filter(item => item.isCheck).reduce((a, c) => a + c.count * c.price, 0))
  const checkCartList = computed(() => cartList.value.filter(item => item.isCheck === true))
  return {
    cartList,
    addCart,
    delCart,
    allCount,
    allPrice,
    updateCheck,
    isAll,
    updateCheckAll,
    checkAllCount,
    checkAllPrice,
    clearCart,
    mergeCart,
    checkCartList,
    updatePayCard
  }
}, {
  persist: true,
})